package web.controller;

import config.SpringContextUtil;
import entity.Classes;
import entity.Department;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import service.ClassesService;
import service.DepartmentService;
import serviceImpl.ClassesServiceImpl;
import serviceImpl.DepartmentServiceImpl;

import java.io.IOException;
import java.util.List;

@WebServlet(name = "classes",urlPatterns = "/classes")
public class ClassesServlet extends HttpServlet {
    private final String classes_list_page = "/WEB-INF/views/instructor/instructor_classes_list.jsp";
    private final String classes_editView_page = "/WEB-INF/views/instructor/instructor_classes_edit.jsp";
    private final String classes_addView_page = "/WEB-INF/views/instructor/instructor_classes_add.jsp";

    @Autowired
    private ClassesService classesService;

    @Override
    public void init() throws ServletException {
        super.init();
        try {
            this.classesService = (ClassesServiceImpl) SpringContextUtil.getBean("classesServiceImpl", ClassesServiceImpl.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        if (action == null) {
            List<Classes> classeses  = classesService.getAllClasses();
            req.setAttribute("classeses", classeses);
            RequestDispatcher dispatcher = req.getRequestDispatcher(classes_list_page);
            dispatcher.forward(req, resp);
        }else if (action.equals("viewEdit")) {
            String classID = req.getParameter("classID");
            req.getSession().setAttribute("classID", classID);
            Classes classes = classesService.getClassesByClassID(classID);
            req.setAttribute("classes", classes);
            RequestDispatcher dispatcher = req.getRequestDispatcher(classes_editView_page);
            dispatcher.forward(req, resp);
        }else if (action.equals("edit")) {
            String classID = req.getSession().getAttribute("classID").toString();
            String className = req.getParameter("className");
            String major = req.getParameter("major");
            String grade = req.getParameter("grade");
            Classes classes = new Classes(classID,className,major,grade);
            boolean flag = classesService.updateClasses(classes);
            if (flag) {
                String message = "修改成功!";
                resp.setContentType("text/html;charset=UTF-8");
                String jsCode = "<script>alert('" + message + "');window.location.href='" + req.getContextPath() + "/classes';</script>";
                resp.getWriter().write(jsCode);
            }
        }else if (action.equals("delete")) {
            String classID = req.getParameter("classID");
            boolean flag = classesService.deleteClasses(classID);
            if (flag) {
                String message = "删除成功!";
                resp.setContentType("text/html;charset=UTF-8");
                String jsCode = "<script>alert('" + message + "');window.location.href='" + req.getContextPath() + "/classes';</script>";
                resp.getWriter().write(jsCode);
            }
        }else if (action.equals("viewAdd")) {
            RequestDispatcher dispatcher = req.getRequestDispatcher(classes_addView_page);
            dispatcher.forward(req, resp);
        }else if(action.equals("add")){
            String classID = req.getParameter("classID");
            String className = req.getParameter("className");
            String depID = req.getParameter("depID");
            String major = req.getParameter("major");
            String grade = req.getParameter("grade");
            Classes classes = new Classes(classID,className,depID,major,grade);
            boolean flag = classesService.addClasses(classes);
            if (flag) {
                String message = "添加成功!";
                resp.setContentType("text/html;charset=UTF-8");
                String jsCode = "<script>alert('" + message + "');window.location.href='" + req.getContextPath() + "/classes';</script>";
                resp.getWriter().write(jsCode);
            }else {
                String message = "添加失败";
                resp.setContentType("text/html;charset=UTF-8");
                String jsCode = "<script>alert('" + message + "');</script>";
                resp.getWriter().write(jsCode);
            }
        }else if(action.equals("search")){
            String keyword = req.getParameter("keyword");
            List<Classes> classeses = classesService.getClassesByClassIDORClassName(keyword,keyword);
            req.setAttribute("classeses", classeses);
            RequestDispatcher dispatcher = req.getRequestDispatcher(classes_list_page);
            dispatcher.forward(req, resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
